home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Amiga Plus 2002 #11
/
Amiga Plus CD - 2002 - No. 11.iso
/
Tools
/
AmigaSystem
/
AmiStart
/
ToolsSDK.lha
/
source
/
ModuleFuncs.c
< prev
next >
Wrap
C/C++ Source or Header
|
2002-06-15
|
19KB
|
751 lines
/*
** $VER: ModuleFuncs.c 39.0 (15.03.02)
**
** Functions for showram.tool
**
** by Darius Brewka, based on Sample Library code "CLib37x" by
** (C) Copyright 1996-97 Andreas R. Kleinert
** All Rights Reserved.
*/
#define __USE_SYSBASE // perhaps only recognized by SAS/C
#include <exec/types.h>
#include <exec/memory.h>
#ifdef __MAXON__
#include <clib/exec_protos.h>
#include <clib/intuition_protos.h>
#include <clib/dos_protos.h>
#include <clib/mui_protos.h>
#else
#include <proto/exec.h>
#include <proto/intuition.h>
#include <proto/dos.h>
#include <proto/muimaster.h>
#include <intuition/intuition.h>
#endif
#include <graphics/layers.h>
#include <libraries/mui.h>
#include "string.h"
#include "compiler.h"
#define PREFS_VERSION 1234
/* Buffer types */
#define TOOLS_TYPE_BITMAP 1
#define TOOLS_TYPE_TRUECOLOR 2
/* Message Flags */
#define TOOLS_FLAG_FMOUSE 1
#define TOOLS_FLAG_FKEY 2
#define TOOLS_FLAG_FTIMER 4
#define TOOLS_FLAG_NOSETUP 8
#define STACKSIZE 2048
#define TASK_PRI 0
#define PaintTool(hd, hook) CallHook(hook, (Object *) hd, NULL)
/* global stuff */
#define ASCLASS_MOUSE 1
#define ASCLASS_TIMER 2
#define ASCLASS_START 3
#define ASCLASS_STOP 4
typedef struct asMessage {
struct Message msg;
UWORD class;
UWORD code;
UWORD x;
UWORD y;
} asMSG;
/* some demo tool specific stuff, you can use it as a startup for your own tools */
#define TIMERDEVICE
#define SECS 0
#define MICROS 250000
#ifdef TIMERDEVICE
struct Device *TimerBase = NULL;
#endif
#define MAXWIDTH 128
#define MINWIDTH 52
#define MAXHEIGHT 64
#define MINHEIGHT 16
struct Library *MUIMasterBase = NULL;
int libcnt = 0;
typedef struct toolsPrefs {
UWORD version;
UWORD width;
UWORD height;
UBYTE transp;
UBYTE textr, textg, textb;
UBYTE noborder;
} tP;
typedef struct newT {
APTR pool;
APTR hd;
ULONG *bm;
ULONG *buffer;
UWORD w;
UWORD h;
BOOL run;
struct Hook *paint;
struct toolsPrefs prefs;
UBYTE p[MAXWIDTH];
/* following is for the time device initialisation */
#ifdef TIMERDEVICE
struct MsgPort *TimePort;
struct timerequest *TimeReq;
#endif
/* following is for creating the message port an the task */
struct Task task;
struct Task *maintask;
struct MsgPort *mp;
ULONG sigstart;
ULONG sigstop;
ULONG sigmask;
ULONG sigmsg;
UBYTE stack[STACKSIZE];
} Tool;
char ToolDescription[] = "Displays the amount of free Memory.";
/* to copy TrueColor data from a source to a destination */
void copyMap(ULONG *s, ULONG *d, UWORD w, UWORD h) {
CopyMem(s, d, w*h*sizeof(ULONG));
}
#define numCols 64
/* a simple gradient for the "level-meter" */
ULONG colors[] = {
0xffAC4A48, 0xffB9605E, 0xffBC685F, 0xffC07061, 0xffC37862, 0xffC78064, 0xffCA8865, 0xffCC9167,
0xffCD9B68, 0xffCFA46A, 0xffD1AD6C, 0xffD2B76D, 0xffD4C06F, 0xffD8C76F, 0xffDDCE70, 0xffE1D570,
0xffE5DC70, 0xffEAE371, 0xffEEEA71, 0xffE8E970, 0xffE2E96F, 0xffDCE86E, 0xffD6E76D, 0xffD0E76C,
0xffCAE66B, 0xffC5E56A, 0xffC0E469, 0xffBBE368, 0xffB5E366, 0xffB0E265, 0xffABE164, 0xffA6E063,
0xff9BDD62, 0xff90DA60, 0xff85D65F, 0xff7AD35E, 0xff6FD05C, 0xff64CD5B, 0xff68CC66, 0xff6CCB70,
0xff70CB7B, 0xff73CA85, 0xff77C990, 0xff7BC89A, 0xff7FCAA6, 0xff82CCB2, 0xff86CFBD, 0xff89D1C9,
0xff8AD0CC, 0xff8CCFCE, 0xff8DCFD1, 0xff8ECED4, 0xff90CDD6, 0xff91CCD9, 0xff8EC4D1, 0xff8ABDC9,
0xff87B5C1, 0xff83AEB9, 0xff80A6B1, 0xff7C9FA9, 0xff7997A1, 0xff759099, 0xff728891, 0xff6E8189
};
char *ramPic[] = {
" 000000000000000000 00000 0000000000000",
" 000000000000000000000 000 0000000000000",
" 000000000000000000000 0 0 0 0000000000000",
" 00 0 00 000 000 00 00 00 00 0 0 0",
" 0000 00 000 0 000 00 00000 0 000 0 0 0 ",
" 0000 000 00 000 00000 0 00 0000 ",
" 0000 000 00000 000000 00000 0 00000 0000 ",
" 0000 0000 000 000 00000 00 00 0000 ",
};
void paintText(Tool *t, char **pic, int x, int y, int w, int h) {
char *p;
ULONG *rgb, col;
int x0, y0, yp;
if (t->buffer == NULL) return;
col = 0xff000000 | t->prefs.textr << 16 | t->prefs.textg << 8 | t->prefs.textb;
rgb = t->buffer;
yp = y * t->w;
for (y0 = 0; y0 < h; y0++) {
p = *pic++;
if (p) {
for (x0 = 0; x0 < w; x0++) {
if (p[x0] == ' ') rgb[x0+x+yp] = col;
}
}
yp += t->w;
}
}
/* create the free-ram "level-meter" */
void makeBar(Tool *t) {
UWORD x, y, y1, ah, w, h;
ULONG *rgb;
ULONG free, total, p, td, q, pen, col;
UWORD r, g, b;
UWORD r0, g0, b0;
free = AvailMem(MEMF_ANY);
total = AvailMem(MEMF_TOTAL);
free = UDivMod32(free, MAXWIDTH);
total = UDivMod32(total, MAXWIDTH);
y1 = t->h - 3;
rgb = t->buffer;
p = UDivMod32(free * (t->w -6), total);
q = 0;
if (p > (t->w - 3)) p = t->w - 3;
w = 42;
h = 8;
x = t->w - w;
y = t->h - h;
paintText(t, ramPic, x >> 1, y >> 1, w, h);
for (x = 3; x < (3 + p); x++) {
ah = 3 * t->w;
pen = t->p[q];
if (pen != 0xff) {
if (pen < 129) col = colors[pen]; else col = 0;
r0 = (col >> 16) & 0xff;
g0 = (col >> 8) & 0xff;
b0 = col & 0xff;
r0 *= 3;
g0 *= 3;
b0 *= 3;
for (y = 3; y < y1; y++) {
r = (r0 + ((rgb[x+ah] >> 16) & 0xff)) >> 2;
g = (g0 + ((rgb[x+ah] >> 8) & 0xff)) >> 2;
b = (b0 + (rgb[x+ah] & 0xff)) >> 2;
rgb[x+ah] = 0xff000000 | r << 16 | g << 8 |b;
ah += t->w;
}
}
q++;
}
}
/* the MessageHandler is the Main Task which recieves Messages form AmiStart and has to handle them */
void SAVEDS ASM __interrupt MessageHandler() {
struct newT *nt;
struct Task *task;
BOOL ok;
asMSG *msg;
ULONG mask;
BOOL running;
task = FindTask(NULL);
nt = (struct newT *) task->tc_UserData;
nt->mp = CreateMsgPort();
nt->sigstop = AllocSignal(-1); // only one signal of 16 -> no test
#ifdef TIMERDEVICE
ok = FALSE;
nt->TimePort = CreateMsgPort();
if (nt->TimePort) {
nt->TimeReq = (struct timerequest *) CreateIORequest(nt->TimePort,sizeof(struct timerequest));
if(nt->TimeReq != NULL) {
if(OpenDevice(TIMERNAME,UNIT_VBLANK,(struct IORequest *) nt->TimeReq,0) == 0) {
if (TimerBase == NULL) TimerBase = nt->TimeReq->tr_node.io_Device;
ok = TRUE;
}
}
}
#endif
Signal(nt->maintask, 1 << nt->sigstart);
if (nt->mp) {
nt->sigmsg = nt->mp->mp_SigBit;
nt->sigmask = (1 << nt->sigstop) | (1 << nt->sigmsg) ;
#ifdef TIMERDEVICE
if (ok) {
nt->sigmask |= (1 << nt->TimePort->mp_SigBit);
nt->TimeReq->tr_node.io_Command = TR_ADDREQUEST;
nt->TimeReq->tr_time.tv_secs = 0;
nt->TimeReq->tr_time.tv_micro = 500000;
SendIO((struct IORequest *) nt->TimeReq);
}
#endif
/************************************************
* MAIN Loop *
************************************************/
running = TRUE;
nt->run = FALSE;
while (running) {
mask = Wait(nt->sigmask);
if ((mask & (1 << nt->sigstop))) running = FALSE;
if ((mask & (1 << nt->sigmsg)) && running) {
/*****************************************************
* you don't need to do a loop, coz only one message *
* will be send *
*****************************************************/
msg = (asMSG *) GetMsg(nt->mp);
if (msg) {
if (msg->class == ASCLASS_STOP) {
nt->run = FALSE;
} else if (msg->class == ASCLASS_START) {
nt->run = TRUE;
copyMap(nt->bm, nt->buffer, nt->w, nt->h);
makeBar(nt);
PaintTool(nt->hd, nt->paint);
} else if (msg->class == ASCLASS_MOUSE && nt->run) {
} else if (msg->class == ASCLASS_TIMER && nt->run) {
}
}
if (msg) ReplyMsg((struct Message *) msg);
}
#ifdef TIMERDEVICE
if (ok && running) {
if (mask & (1 << nt->TimePort->mp_SigBit)) {
WaitIO((struct IORequest *) nt->TimeReq);
if (nt->run) {
copyMap(nt->bm, nt->buffer, nt->w, nt->h);
makeBar(nt);
PaintTool(nt->hd, nt->paint);
}
nt->TimeReq->tr_node.io_Command = TR_ADDREQUEST;
nt->TimeReq->tr_time.tv_secs = SECS;
nt->TimeReq->tr_time.tv_micro = MICROS;
SendIO((struct IORequest *) nt->TimeReq);
}
}
#endif
}
} else {
Wait(1 << nt->sigstop);
}
FreeSignal(nt->sigstop);
#ifdef TIMERDEVICE
if (ok) {
AbortIO((struct IORequest *) nt->TimeReq);
WaitIO((struct IORequest *) nt->TimeReq);
CloseDevice((struct IORequest *) nt->TimeReq);
}
if (nt->TimeReq) DeleteIORequest((struct IORequest *) nt->TimeReq);
if (nt->TimePort) DeletePort(nt->TimePort);
#endif
if (nt->sigstart != -1) Signal(nt->maintask, 1 << nt->sigstart);
if (nt->mp) DeleteMsgPort(nt->mp);
Wait(0);
}
BOOL SAVEDS ASM OpenTool() {
BOOL back = FALSE;
if (MUIMasterBase == NULL) {
MUIMasterBase = OpenLibrary("muimaster.library", 19);
}
if (MUIMasterBase) {
libcnt++;
back = TRUE;
}
return back;
}
void SAVEDS ASM CloseTool() {
if (MUIMasterBase) {
libcnt--;
if (libcnt <= 0) {
CloseLibrary(MUIMasterBase);
MUIMasterBase = NULL;
}
}
}
STRPTR SAVEDS ASM ToolInfo() {
return ToolDescription;
}
APTR SAVEDS ASM NewTool(REG(a0) tP *prefs, REG(a1) APTR pool) {
struct newT *nt;
BOOL usedefault;
UBYTE *s, *d;
int p;
nt = AllocPooled(pool, sizeof(struct newT));
if (nt) {
usedefault = TRUE;
if (prefs) {
/* prefs are passed by AmiStart */
if (prefs->version == PREFS_VERSION) {
s = (UBYTE*) prefs;
d = (UBYTE*) &nt->prefs;
for (p = 0; p < sizeof(tP); p++) *d++ = *s++;
nt->prefs.width = prefs->width;
nt->prefs.height = prefs->height;
usedefault = FALSE;
}
}
if (usedefault) {
/* if no prefs are passed use default prefs */
nt->prefs.width = MINWIDTH;
nt->prefs.height = MINHEIGHT;
}
if (nt->prefs.width < MINWIDTH) nt->prefs.width = MINWIDTH;
if (nt->prefs.width > MAXWIDTH) nt->prefs.width = MAXWIDTH;
if (nt->prefs.height < MINHEIGHT) nt->prefs.height = MINHEIGHT;
if (nt->prefs.height > MAXHEIGHT) nt->prefs.height = MAXHEIGHT;
nt->sigstart = AllocSignal(-1);
if (nt->sigstart != -1) {
nt->pool = pool;
nt->task.tc_Node.ln_Pri = TASK_PRI;
nt->task.tc_Node.ln_Type = NT_TASK;
nt->task.tc_SPLower = (APTR) nt->stack;
nt->task.tc_SPUpper = (APTR) ((ULONG) nt->stack+STACKSIZE);
nt->task.tc_SPReg = (APTR) ((ULONG) nt->stack+STACKSIZE);
nt->task.tc_UserData = nt;
nt->maintask = FindTask(NULL);
AddTask(&nt->task, (APTR) MessageHandler, NULL); /* create the Tool Task */
Wait(1 << nt->sigstart);
if (nt->mp == NULL) {
Signal(&nt->task, 1 << nt->sigstop);
if (nt->sigstart != -1) Wait (1 << nt->sigstart);
RemTask(&nt->task);
}
FreeSignal(nt->sigstart);
} else {
FreePooled(pool, nt, sizeof(struct newT));
nt = NULL;
}
}
return nt;
}
UWORD SAVEDS ASM ToolWidth(REG(a0) Tool *tool, REG(d0) UWORD width) {
tool->w = tool->prefs.width;
return tool->prefs.width;
}
UWORD SAVEDS ASM ToolHeight(REG(a0) Tool *tool, REG(d0) UWORD height) {
tool->h = tool->prefs.height;
return tool->prefs.height;
}
void SAVEDS ASM InitTool(REG(a0) Tool *tool, REG(a1) APTR bm, REG(a2) APTR buffer, REG(a3) struct Hook *hook, REG(a4) APTR t) {
ULONG *raw, rgb, td, q, pen;
int x,y,h,w;
BOOL f0,f1;
tool->bm = (ULONG *) bm;
tool->buffer = (ULONG *) buffer;
tool->hd = t;
tool->paint = hook;
w = tool->w;
h = tool->h;
/* make some visula initializations, and initialize the level-meter buffers */
if (tool->bm && tool->buffer) {
raw = (ULONG* ) buffer;
if (!tool->prefs.noborder) {
for (x=0; x < w; x++) {
raw[x] = 0xff888888;
raw[x+(h-1)*w] = 0xff888888;
}
for (x=1; x < (w-1); x++) {
raw[x+w] = 0xff444444;
raw[x+(h-2)*w] = 0xff444444;
}
for (y=0; y < h; y++) {
raw[y*w] = 0xff888888;
raw[y*w+w-1] = 0xff888888;
}
for (y=1; y < (h-1); y++) {
raw[y*w+1] = 0xff444444;
raw[y*w+w-2] = 0xff444444;
}
}
if (!tool->prefs.transp) {
f1 = FALSE;
for (y = 2; y < (h-2) ; y++) {
f0 = f1;
for (x = 2; x < (w-2); x++) {
if (f1) {
if (f0) rgb = 0xffcccccc; else rgb = 0xffaaaaaa;
} else {
if (f0) rgb = 0xffbbbbbb; else rgb = 0xff999999;
}
raw[x+y*w] = rgb;
f0 = !f0;
}
f1 = !f1;
}
}
copyMap(tool->buffer, tool->bm, tool->w, tool->h);
w -= 6;
td = w;
for (q = 0; q < w; q++) {
pen = UDivMod32(q*numCols, td);
if (pen >= numCols) pen = numCols - 1;
tool->p[q] = (UBYTE) pen;
}
/*
w = UDivMod32(w, 3);
for (q = 0; q < w; q++) {
pen = UDivMod32(q*3*numCols, td);
if (pen >= numCols) pen = numCols - 1;
tool->p[q*3+0] = (UBYTE) pen;
tool->p[q*3+1] = (UBYTE) pen;
tool->p[q*3+2] = (UBYTE) pen; //0xfe; //pen;
}
*/
}
}
void SAVEDS ASM DisposeTool(REG(a0) Tool *tool) {
if (tool) {
tool->sigstart = AllocSignal(-1);
Signal(&tool->task, 1 << tool->sigstop);
if (tool->sigstart != -1) Wait (1 << tool->sigstart);
RemTask(&tool->task); /* stop the Tool Task */
if (tool->sigstart != -1) FreeSignal(tool->sigstart);
FreePooled(tool->pool, (UBYTE*) tool, sizeof(struct newT));
}
}
ULONG SAVEDS ASM GetToolFlags(REG(a0) Tool *tool) {
// return TOOLS_FLAG_FMOUSE|TOOLS_FLAG_NOSETUP;
return 0;
}
struct MsgPort SAVEDS ASM *GetToolPort(REG(a0) Tool *tool) {
return tool->mp;
}
UWORD DisplayToolSetup(Tool *);
UWORD SAVEDS ASM ToolSetup(REG(a0) Tool *tool, REG(d0) UWORD MouseX, REG(d1) UWORD MouseY) {
return DisplayToolSetup(tool);
}
APTR SAVEDS ASM GetToolPrefs(REG(a0) Tool *tool) {
tool->prefs.version = PREFS_VERSION;
// tool->prefs.width = tool->w;
// tool->prefs.height = tool->h;
return (APTR) &tool->prefs;
}
int SAVEDS ASM GetToolPrefsSize(REG(a0) Tool *tool) {
return sizeof(struct toolsPrefs);
}
UWORD SAVEDS ASM GetToolBufferType(REG(a0) Tool *tool) {
return TOOLS_TYPE_TRUECOLOR;
}
#define SP_TRANSPARENT 1
#define SP_TEXTCOLOR 2
#define SP_NOBORDER 3
Object *PenObject(UBYTE r, UBYTE g, UBYTE b) {
return ColorfieldObject,
ImageButtonFrame,
MUIA_CycleChain, 1,
MUIA_InputMode , MUIV_InputMode_RelVerify,
MUIA_Colorfield_Red, r << 24,
MUIA_Colorfield_Green, g << 24,
MUIA_Colorfield_Blue, b << 24,
End;
}
UWORD DisplayToolSetup(Tool *tool) {
Object *app, *win;
Object *transpObject, *noborderObject, *wObject, *hObject, *textcolorObject;
Object *textcolorwin, *textpenObject, *textcolorokObject, *textcolorcancelObject;
BOOL running = TRUE;
ULONG signals;
ULONG rtsm, id, nr;
UWORD back;
back = 0;
if (MUIMasterBase == NULL) return 0;
app = ApplicationObject,
MUIA_Application_Title, "Tools Setup",
MUIA_Application_Base, "AMISTART",
SubWindow, win = WindowObject,
MUIA_Window_Activate, TRUE,
MUIA_Window_DepthGadget, TRUE,
MUIA_Window_DragBar, TRUE,
MUIA_Window_SizeGadget, TRUE,
MUIA_Window_AppWindow, TRUE,
MUIA_Window_CloseGadget, TRUE,
MUIA_Window_Borderless, FALSE,
MUIA_Window_Title, "Show Ram ToolSetup",
MUIA_Window_TopEdge, MUIV_Window_TopEdge_Moused,
MUIA_Window_LeftEdge, MUIV_Window_LeftEdge_Moused,
WindowContents, VGroup,
Child, HGroup,
Child, Label2("Transparent"),
Child, HVSpace,
Child, transpObject = CheckMark(tool->prefs.transp),
End,
Child, HGroup,
Child, Label2("No Border"),
Child, HVSpace,
Child, noborderObject = CheckMark(tool->prefs.noborder),
End,
Child, ColGroup(2),
Child, Label2("Width:"),
Child, wObject = Slider(MINWIDTH, MAXWIDTH, tool->prefs.width),
Child, Label2("Height:"),
Child, hObject = Slider(MINHEIGHT, MAXHEIGHT, tool->prefs.height),
Child, Label2("Text Color:"),
Child, textcolorObject = PenObject(tool->prefs.textr, tool->prefs.textg, tool->prefs.textb),
End,
End,
End,
SubWindow, textcolorwin = WindowObject,
MUIA_Window_Open, FALSE,
MUIA_Window_CloseGadget, FALSE,
WindowContents, VGroup,
Child, textpenObject = ColoradjustObject,
MUIA_Coloradjust_Red, tool->prefs.textr << 24,
MUIA_Coloradjust_Green, tool->prefs.textg << 24,
MUIA_Coloradjust_Blue, tool->prefs.textb << 24,
End,
Child, ColGroup(2),
Child, textcolorokObject = KeyButton("OK",0),
Child, textcolorcancelObject = KeyButton("Cancel",0),
End,
End,
End,
End;
if (app) {
DoMethod(win,MUIM_Notify,MUIA_Window_CloseRequest,TRUE,app,2,MUIM_Application_ReturnID,MUIV_Application_ReturnID_Quit);
DoMethod(transpObject,MUIM_Notify,MUIA_Selected,MUIV_TriggerValue,app,2,MUIM_Application_ReturnID,SP_TRANSPARENT);
DoMethod(noborderObject,MUIM_Notify,MUIA_Selected,MUIV_TriggerValue,app,2,MUIM_Application_ReturnID,SP_NOBORDER);
DoMethod(textcolorObject, MUIM_Notify, MUIA_Pressed, TRUE, textcolorwin, 4, MUIM_Set, MUIA_Window_Open, TRUE);
DoMethod(textcolorcancelObject,MUIM_Notify,MUIA_Selected, MUIV_TriggerValue, textcolorwin, 4, MUIM_Set, MUIA_Window_Open, FALSE);
DoMethod(textcolorokObject,MUIM_Notify,MUIA_Pressed, TRUE,app,2,MUIM_Application_ReturnID,SP_TEXTCOLOR);
set(win,MUIA_Window_Open,TRUE);
while (running) {
id = DoMethod(app,MUIM_Application_Input,&signals);
switch (id) {
case MUIV_Application_ReturnID_Quit:
get(wObject, MUIA_Slider_Level, &rtsm);
tool->prefs.width = (UWORD) rtsm;
get(hObject, MUIA_Slider_Level, &rtsm);
tool->prefs.height = (UWORD) rtsm;
running = FALSE;
break;
case SP_TRANSPARENT:
get(transpObject,MUIA_Selected,&rtsm);
if (rtsm) tool->prefs.transp = TRUE; else tool->prefs.transp = FALSE;
break;
case SP_NOBORDER:
get(noborderObject,MUIA_Selected,&rtsm);
if (rtsm) tool->prefs.noborder = TRUE; else tool->prefs.noborder = FALSE;
break;
case SP_TEXTCOLOR:
set(textcolorwin, MUIA_Window_Open, FALSE);
get(textpenObject,MUIA_Coloradjust_Red, &rtsm);
set(textcolorObject, MUIA_Colorfield_Red, rtsm);
tool->prefs.textr = (UBYTE) ((rtsm >> 24) & 0xff);
get(textpenObject,MUIA_Coloradjust_Green, &rtsm);
set(textcolorObject, MUIA_Colorfield_Green, rtsm);
tool->prefs.textg = (UBYTE) ((rtsm >> 24) & 0xff);
get(textpenObject,MUIA_Coloradjust_Blue, &rtsm);
set(textcolorObject, MUIA_Colorfield_Blue, rtsm);
tool->prefs.textb = (UBYTE) ((rtsm >> 24) & 0xff);
break;
} if (running && signals) Wait(signals);
}
set(win,MUIA_Window_Open,FALSE);
MUI_DisposeObject(app);
}
return back;
}